package morphage.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import morphage.engine.ImageManager;

public class AppRenderViewListener extends MouseAdapter implements MouseWheelListener, MouseListener, MouseMotionListener
{
  private static final String       POPUP_CHANGE_IMAGE_STR = "Load Image...";
  
	private AppRenderViewPanel 		  	m_panel;
	private JPopupMenu 						  	m_popup;
	private final JFileChooser        m_imageFileChooser = new JFileChooser();
	
	
	public AppRenderViewListener(AppRenderViewPanel panel)
	{
		assert( panel != null );
		
		m_panel       = panel;
		
		// Image file chooser
		m_imageFileChooser.setFileFilter( new ImageFileFilter() );
		m_imageFileChooser.setCurrentDirectory( new File(System.getProperty("user.dir")) );
		
		// popup
		if( m_panel.getViewSide() != AppRenderViewPanel.CENTER_VIEW )
		{
  		JMenuItem               menuItem;
  		m_popup = new JPopupMenu();    
      menuItem = new JMenuItem( POPUP_CHANGE_IMAGE_STR );
      PopupActionListener     popupAL = new PopupActionListener( menuItem, m_panel );
      menuItem.addActionListener( popupAL );
      m_popup.add( menuItem );
		}
	}
	
	public void mouseWheelMoved(MouseWheelEvent e) 
	{
	}
	public void mouseDragged(MouseEvent e)
	{
	}
	public void mousePressed	(MouseEvent e)
	{
		maybeShowPopup(e);
	}
	public void mouseReleased	(MouseEvent e) //{ /* DO NOTHING */ }
	{
		maybeShowPopup(e);
	}
	public void mouseEntered	(MouseEvent e) { /* DO NOTHING */ }
	public void mouseExited		(MouseEvent e) { /* DO NOTHING */ }
	public void mouseMoved		(MouseEvent e) { /* DO NOTHING */ }

	private void maybeShowPopup(MouseEvent e) 
	{
	  if( m_popup != null )
	    if (e.isPopupTrigger())
	      m_popup.show(e.getComponent(), e.getX(), e.getY());
	}
	
	private class PopupActionListener implements ActionListener
	{
	  private JComponent           m_parent;
	  private AppRenderViewPanel   m_renderViewPanel;
	  
	  public PopupActionListener( JComponent parent, AppRenderViewPanel renderViewPanel )
	  {
	    assert( parent != null );
	    
	    m_parent           = parent;
	    m_renderViewPanel  = renderViewPanel;
	  }
	  public void actionPerformed( ActionEvent e )
	  {
	    if( e.getActionCommand().equals( POPUP_CHANGE_IMAGE_STR ) )
	    {
	      if( m_renderViewPanel.getViewSide() == AppRenderViewPanel.CENTER_VIEW )
	        return;
	      int returnVal = m_imageFileChooser.showOpenDialog( m_parent );
	      
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
          File file = m_imageFileChooser.getSelectedFile();
          try
          {
            if( m_renderViewPanel.getViewSide() == AppRenderViewPanel.LEFT_VIEW )
              ImageManager.loadFirstImage( file.getPath() );
            else if( m_renderViewPanel.getViewSide() == AppRenderViewPanel.RIGHT_VIEW )
              ImageManager.loadLastImage( file.getPath() );
            
            m_renderViewPanel.repaint();
          }
          catch(Exception ex)
          {
              JOptionPane.showMessageDialog( GUI.getFrame(),
                      ex.getLocalizedMessage());
              ex.printStackTrace();
          }
        } 
	    }
	  }
	}
	
	private class ImageFileFilter extends javax.swing.filechooser.FileFilter
  {
      public boolean accept(File f)
      {
          return true;
      }
      public String getDescription()
      {
          return "All image files";
      }
  }
}
